package com.mes.web.controller.cb;

import com.mes.service.cb.OrderService;
import com.mes.service.cb.SubmiTaskService;
import com.mes.utils.ResponseUtil;
import net.sf.json.JSONObject;
import org.activiti.engine.*;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: MaterieReturnController
 * @Description: 采购退货审批
 * @Author cb
 * @Date 2018年3月30日15:58:39
 * @version 1.0
 */
@Controller
@RequestMapping("/maReturn")
public class MaterieReturnController {


    @Resource
    private TaskService taskService;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private FormService formService;

    @Resource
    private HistoryService historyService;


    @Resource
    private OrderService orderService;


    @Resource
    private SubmiTaskService submiTaskService;

    /**
     * 经理审核(采购订单)
     * @param
     * @param taskId 任务id
     * @param comments 批注
     * @param state 状态 1--通过 2--不通过
     * @param response
     * @return
     * @throws Exception
     */

    @RequestMapping("/retuenManager")
    public String audit_bz(Long pcId,String taskId,String comments,Integer state,HttpServletResponse response)throws Exception{


        JSONObject result = getJsonObject(pcId, taskId, comments, state);

        ResponseUtil.write(response, result);

        return null;
    }


    /**
     * 厂长审批
     * @param
     * @param taskId
     * @param comments
     * @param state
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/returnGman")
    public String returnGman(Long pcId,String taskId,String comments,Integer state,HttpServletResponse response)throws Exception{




        //JSONObject result = getJsonObject(pcId, taskId, comments, state);
        if (state == 1) {
            //将"审批中"改为审批通过
            Map<String, Object> map = new HashMap<>();
            map.put("rid", pcId);
            map.put("state", "3");
            map.put("prremark2",null);




            int total = submiTaskService.returenMaterie_updateByState(map);


            System.out.println(total);
        }


        //ResponseUtil.write(response, result);

        return null;
    }

    private JSONObject getJsonObject(Long pcId, String taskId, String comments, Integer state) {
        Task task=taskService.createTaskQuery() // 创建任务查询
                .taskId(taskId) // 根据任务id查询
                .singleResult();

        JSONObject result=new JSONObject();

        Map<String,Object> variables=new HashMap<>();
        if (null != task) {

            //流程ID
            String processInstanceId = task.getProcessInstanceId();
            if (state == 1) {
                variables.put("msg", "同意");

            } else {

                //审核未通过,将"审批中"改为审批未通过
                Map<String, Object> map = new HashMap<>();
                map.put("rid", pcId);
                map.put("state", "3");
                int total = submiTaskService.returenMaterie_updateByState(map);


                variables.put("msg", "驳回");

            }

            // 批注人
            Authentication.setAuthenticatedUserId("12");
            // 添加批注信息
            taskService.addComment(taskId,processInstanceId, comments);
            // 完成审批
            taskService.complete(task.getId(), variables);


            result.put("success", 1);

        } else {
            result.put("success", 2);
        }
        return result;
    }

}
